<?php
namespace App\Admin\Controllers\Traits\WxVoterTraits;

use App\Admin\Renderable\BannerRender;
use App\Admin\Renderable\IconRender;
use App\Models\Voter\WxVoterTemplate;
use App\Models\WxIcon;
use App\Models\WxSlideshow;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formVoterTrait{

    public function index_config(Content $content)
    {
        return $content->title('选票系统配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_config()->render()}</div>
HTML
            );
    }
    protected function form_config(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();

        $form->tab('选票首页', function (Form $form) {
            $form->multipleSelectTable('app_voter_header_icon', '头部左右侧')
                ->title('图标库')
                ->max(2)
                ->from(IconRender::make())
                ->model(WxIcon::class, 'id', 'name', 'selectTable:formVoterTrait:app_voter_header_icon')
                ->help('添加两个，分别是左右侧，小程序不显示右侧， <a href="'.admin_url('settings/icons').'" target="_blank">管理图标库>></a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_voter_header_icon', '', true));


            $form->multipleSelectTable('app_voter_banner', '轮播图')
                ->title('轮播图库')
                ->max(10)
                ->from(BannerRender::make())
                ->model(WxSlideshow::class, 'id', 'id', 'selectTable:formVoterTrait:app_voter_banner')
                ->help('<a href="'.admin_url('slideshow').'" target="_blank">运营管理-轮播图中心</a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_voter_banner', '',true));

            $form->divider();

            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block"><img src="https://img.mini.minisns.cn/images/page/voter.png" style="width: 250px;"></div>';
            }, '');


            $form->table('app_voter_tab','tab自定义', function (NestedForm $table) {
                $table->text('name', '名字')->required();
                $table->multipleSelect('template', '模板')->options(WxVoterTemplate::where('status', 1)->pluck('name', 'id'));
                $table->select('orderBy', '排序依据')->options(['created_at'=>'创建时间'])->default('created_at');
                $table->radio('order', '排序方向')->options(['desc'=>'倒排', 'asc'=>'正排'])->default('desc');
                $table->switch('master', '当前用户创建')->default(0);
                $table->switch('player', '当前用户报名选手')->default(0);
                $table->switch('voted', '当前用户已投票')->default(0);
            })->default(Settings::get('app_voter_tab', '', true));

        });

        $form->tab('选票详情', function (Form $form) {
            $form->checkbox('app_voter_social_share_types', '社交分享')->options([
                'refer' => '转发',
                'wechat' => '微信',
                'timeline' => '朋友圈',
                'qq' => 'QQ',
                'qzone' => 'QQ空间',
                'weibo' => '微博',
            ])->default(Settings::get('app_voter_social_share_types', ['refer', 'wechat', 'timeline', 'qq', 'qzone', 'weibo']));
            $form->checkbox('app_voter_social_system_types', '系统功能')->options([
//                'showCode' => '生成海报',
                'edit' => '编辑',
                'copyUrl' => '复制链接',
                'report' => '举报',
            ])->default(Settings::get('app_voter_social_system_types', ['edit', 'copyUrl', 'report']));
        });

        $form->action('voter/action/config');
        return $form;
    }

    public function config(Request $request){
        SiteUtils::forget_config_data();

        $app_voter_tab = AdminUtils::_table_data($request->input('app_voter_tab'));

        Settings::set('app_voter_header_icon', AdminUtils::_multipleSelectTable_order_process( 'selectTable:formVoterTrait:app_voter_header_icon', $request->input('app_voter_header_icon') ), true, true);
        Settings::set('app_voter_banner', AdminUtils::_multipleSelectTable_order_process('selectTable:formVoterTrait:app_voter_banner', $request->input('app_voter_banner')), true, true);
        Settings::set('app_voter_tab', $app_voter_tab, true, true);

        Settings::set('app_voter_social_share_types', $request->input('app_voter_social_share_types'), true);
        Settings::set('app_voter_social_system_types', $request->input('app_voter_social_system_types'), true);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
